package com.furentech.iotserver.fa.sinopec.sinopec_processor;

import com.furentech.iotserver.fa.server.ChannelUtil;
import com.furentech.iotserver.fa.sinopec.SinopecFunCode;
import io.netty.channel.Channel;
import org.springframework.stereotype.Component;

@Component
public class ReqDebit_Processor extends SinopecProcessorBase{
    @Override
    String actualGetJsonString(byte[] buf) {
        return null;
    }

    @Override
    String actualGetReadableString(byte[] buf) {
        return "加油机上传加油结果\r\n";
    }

    @Override
    byte[] actualGetResponse(byte[] buf, Channel channel) {
        byte[] tempByteBuf = new byte[170];

        tempByteBuf[0] = SinopecFunCode.Resp_Debit;

        //StationID NZN
        System.arraycopy(buf, 1, tempByteBuf, 1, 5);

        //POSTTC
        System.arraycopy(buf, 8, tempByteBuf, 6, 4);

        //AppID
        System.arraycopy(buf, 72, tempByteBuf, 10, 2);

        //AMN
        System.arraycopy(buf, 63, tempByteBuf, 12, 3);

        //ErrorCode
        tempByteBuf[15] = 0x00;

        //ErrorMsg
        for (int i = 0; i < 50; i++) {
            tempByteBuf[16 + i] = 0x20;
        }

        //InvoiceURL
        for (int i = 0; i < 100; i++) {
            tempByteBuf[66 + i] = 0x20;
        }

        //MAC
        System.arraycopy(buf, 90, tempByteBuf, 166, 4);

        Byte[] tempBytes = new Byte[tempByteBuf.length];
        for (int i = 0; i < tempByteBuf.length; i++) {
            tempBytes[i]=tempByteBuf[i];
        }
        ChannelUtil.setReqDebitRespBuf(channel, tempBytes);
        ChannelUtil.setReqDebitFlag(channel, true);

        byte[] resByteBuf = new byte[4];

        resByteBuf[0] = SinopecFunCode.CMD_ACK;
        resByteBuf[1] = buf[0];
        resByteBuf[2] = buf[5];
        resByteBuf[3] = 0x02;

        return resByteBuf;
    }
}
